using System;
using TMPro;
using UnityEngine;
using Utils;

public class Melee : MonoBehaviour
{
    public int atk;
    public bool canAttack = false;
    public Collider2D activeCollider;

    public GameObject damageTextGO;
    
    public void SwitchCanAttack()
    {
        canAttack = true;
        activeCollider.enabled = false;
        // print("CanAttack has Switched");
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (!canAttack && other.gameObject.tag.Equals("Enemy"))
        {
            Monster monster = other.transform.GetComponent<Monster>();
            if (monster)
            {
                CallDamageText(other.transform);
                monster.hp -= atk;
                monster.BeAttacked();
            }
            else
            {
                other.gameObject.SetActive(false);
            }
        }
    }
    
    private void CallDamageText(Transform tr)
    {
        damageTextGO = UITool.InstantiatePrefabUI(PrefabRoot.DamageText.Path, PrefabRoot.DamageText.Name);
        damageTextGO.GetComponent<TextMeshProUGUI>().text = atk.ToString();
        damageTextGO.SetActive(true);
        damageTextGO.tag = tag;
        damageTextGO.transform.position = GameController.Instance().GetMainCamera().WorldToScreenPoint(tr.position);
        Destroy(damageTextGO,0.5f);
    }
}
